<?php
	/**
	 * Created by PhpStorm.
	 * User: Administrator
	 * Date: 2017/10/24
	 * Time: 15:45
	 * 此方式 是 使用行为钩子的方式 实现 全局配置 登录检查
	 */
	namespace app\index\behavior;
	use app\index\api\UserApi;
	use think\Controller;  //继承 Controller后 可以使用 assign() 来 替换 模板中的 变量
	use think\Request;

	class UserBehavior extends Controller
	{
		public $request=null;
		public $userapi=null;

		public function _initialize()
		{
			parent::_initialize(); // TODO: Change the autogenerated stub
			$this->request = Request::instance();
			$this->userapi = new UserApi();
		}

		public function run(&$params)
		{
			$module = $this->request->module();
			$controller = $this->request->controller();
			$config = config('config');

			if(array_key_exists($module,$config['need_auth'])&&in_array($controller,$config['need_auth'][$module])){
				if($this->userapi->user_status()){
					$this->assign('user_status',$this->userapi->user_status()->user_name);
				}else{
					$this->error('请先登录','index/user/login');
				}
			}
			if($this->userapi->user_status()){
				$this->assign('user_status',$this->userapi->user_status()->user_name);
			}

		}

	}